package totask.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import totask.pojo.TotaskUser;
import totask.response.ResponseData;
import totask.service.impl.UserServiceImpl;
import totask.vo.UserInfoVO;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import java.util.List;

import static totask.response.ResultCode.USER_ACCOUNT_ADD_MISTAKE;


@Api(tags = "用户操作")
@RestController
@Validated
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserServiceImpl userService;

    @ApiOperation("获取所有用户")
    @PostMapping("/getUserAll")
    public ResponseData<List<TotaskUser>> getUserAll(){
        return new ResponseData<List<TotaskUser>>().success(userService.getUserAll());
    }

    /**
     * TotaskUser
     * 只需要三个参数：username、password、totaskRole
     */
    @ApiOperation("添加用户")
    @PostMapping("/addUser")
    public ResponseData addUser(@Valid @RequestBody TotaskUser totaskUser){
        boolean userFlag = userService.addUser(totaskUser);
        if (userFlag){
            return new ResponseData().success();
        }
        return new ResponseData().fail(USER_ACCOUNT_ADD_MISTAKE);
    }

    @ApiOperation("根据账号或密码登录")
    @PostMapping("/userLogin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户账号", dataType = "String", required = true),
            @ApiImplicitParam(name = "passWord", value = "用户密码", dataType = "String", required = true)
    })
    public ResponseData<UserInfoVO> userLogin(  @RequestParam @NotBlank(message = "用户名不能为空") String userName,
                                               @RequestParam @NotBlank(message = "用户密码不能为空") String passWord){
        //如果结果为null，则账号密码错误！
        return new ResponseData<UserInfoVO>().success(userService.userLogin(userName, passWord));
    }
    @ApiOperation("根据用户id获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String", required = true),
    })
    @PostMapping("/getByUser")
    public ResponseData<TotaskUser> getByUser( @RequestParam @NotBlank(message = "用户id不能为空") String userId){
        return new ResponseData<TotaskUser>().success(userService.getByUser(userId));
    }

    @ApiOperation("根据角色id查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "Integer", required = true),
    })
    @PostMapping("/queryRoleUser")
    public ResponseData<List<TotaskUser>> queryRoleUser( @RequestParam  @Min(value = 1, message ="角色id不能小于1") Integer roleId){
        return new ResponseData<List<TotaskUser>>().success(userService.queryRoleUser(roleId));
    }

}